import java.util.Arrays;
import java.util.LinkedList;

/**
 * @author wangj881
 * @date 2024-12-10 18:58
 */
public class L1047_2 {
    public static void main(String[] args) {
        String abbaca = new Solution().removeDuplicates("abbaca");
        System.out.println(abbaca);

    }

    static class Solution {
        public String removeDuplicates(String s) {
            LinkedList<Character> list = new LinkedList<>();
            for (char c : s.toCharArray()) {
                if (list.size() > 0) {
                    if (list.peekLast().equals(c)) {
                        list.removeLast();
                        continue;
                    }
                }
                list.add(c);
            }
            StringBuilder builder = new StringBuilder();
            for (Character character : list) {
                builder.append(character);
            }
            return builder.toString();
        }
    }
}
